package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import qeug.shsh.bpm.Factory.ThirdServiceFactory;
import qeug.shsh.bpm.service.SAPService;

import java.util.List;

public class VoucherSAPValidator extends AbstractValidator {
    @Override
    public void validate() {
        try {
            ExtendedDataEntity[] dataEntities1 = this.getDataEntities();
            for (ExtendedDataEntity extendedDataEntity : dataEntities1) {
                DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                Long id = dataEntity.getLong("id");
                QFilter[] qFilters = new QFilter[]{new QFilter("id", "=", id)};

                DynamicObject gl_voucher = BusinessDataServiceHelper.loadSingle("gl_voucher", qFilters);
                DynamicObject org = gl_voucher.getDynamicObject("org");//核算组织
                String orgId = org.getString("id");
                long orgIdLong = Long.parseLong(orgId);
                QFilter[] qFilter = new QFilter[]{
                        new QFilter("id", QCP.equals, orgIdLong)
                };
                DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "qeug_issap", qFilter);
                boolean isSap = bos_org.getBoolean("qeug_issap");//是否推送SAP标识
                //判断组织中字段“是否推送SAP”为是还是否，为是才能推送SAP
                if (isSap) {

                } else {
//                throw new IllegalArgumentException();
                    this.addErrorMessage(extendedDataEntity, "当前核算组织不能推送SAP!");

                }
                SAPService sapService =  ThirdServiceFactory.createSAPService();
                String voucher = sapService.voucher(gl_voucher);
                this.addMessage(extendedDataEntity, voucher);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
